<template>

  <div class="login-wrap">
        <!-- 左侧 -->
        <div class="left_cent">
          <el-image fit="cover" style="width:800px;height:902px" :src="require('@/assets/left.png')" />
        </div>
        <!-- 右侧 -->
        <div class="right_cent">
           <div class="ms-login">
            <h4 class="title">用户登录</h4>
            <el-form :model="loginForm" ref="loginForm" :rules="rules" label-width="0px">
                <el-form-item prop="account" >
                   <el-input type="text" ref="account" 
                    v-model="loginForm.account" name="account" required data-msg="请输入账户名称" 
                    placeholder="账户名称"
                  />
                </el-form-item>

                <el-form-item prop="password">
                    <el-input ref="password" type="password" 
                      v-model="loginForm.password"  name="passWord" required data-msg="请输入密码" 
                      placeholder="登录密码"
                      @keyup.enter.native="handleLogin"
                    />
                </el-form-item>

                <!-- <el-form-item >
                   <div>
                     <el-input type="text" ref="account" 
                      v-model="loginForm.account" name="account" required data-msg="请输入账户名称" 
                      placeholder="账户名称" 
                    />
                   </div>
                </el-form-item> -->
            </el-form>
            <div class="login-btn">
                  <el-button  @click="handleLogin" type="primary" :disabled="loading" :loading="loading">确认登录</el-button>
              </div>
          </div>
        </div>
    </div>
</template>

<script>
import{sendCode} from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        account: '',
        password: '',
        imgCode:'',
        mobile:'',
        uuid:'',
        code:''
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,

      rules: {
          account: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
          password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
      rules2: {
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证', trigger: 'blur' }],
      },
      isTrue:true,
      isCode:false,
      title:"获取验证码",
      titlename:'手机号登录'
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleCeck(){
      this.isTrue = !this.isTrue
      if(this.isTrue){
        this.titlename = "手机号登录"
        this.loginForm.mobile = ""
        this.loginForm.code = ""
      }else{
        this.titlename = "账号登录"
        this.loginForm.account = ""
        this.loginForm.password = ""
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handlesendCode(){
      let i = 30
      let tiems = setInterval(() => {
          this.isCode =true
          i--
          if(i<=0){
            this.title = "获取验证码"
            this.isCode = false
            clearInterval(tiems);
          }else{
            this.title = i + '秒后获取验证'
          }
      },1000);
      sendCode({mobile:this.loginForm.mobile}).then(res=>{
        if(res.status==200){
          this.$message({
            message: res.msg,
            type: 'success'
          });
        }else{
          this.$message({
            message: res.msg,
            type: 'error'
          });
          this.title = "获取验证码"
          this.isCode = false
          clearInterval(tiems);
        }
      })
    },
    handleLogin() {
      if(this.isTrue){
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/login', this.loginForm).then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            }).catch((error) => {
              this.$notify({ title: "操作失败",message: '账户密码不对', type: "error", duration: 2000,});
              this.loading = false
            })
          } else {
            console.log('提交失败!!')
            return false
          }
        })
      }else{
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/login', this.loginForm).then(() => {
              this.$router.push({ path: this.redirect || '/home' })
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          } else {
            console.log('提交失败!!')
            return false
          }
        })
      }

    }
  }
}
</script>

<style scoped lang="scss">
  .login-wrap {
      width: 100%;
      height: 100vh;
      display: flex;
  }
  .left_cent{
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #6594EE;
  }
  .right_cent{
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #FAFAFA;
  }
  .ms-login {
      width: 450px;
      height:400px;
      border-radius: 12px;
      background-color: #fff;
      overflow: hidden;
      padding: 20px 30px 0px 30px;
      box-shadow: 0 12px 20px rgba(0, 0, 0, .08);
      .title{
        color: #769ae9;
        font-size: 22px;
        margin-bottom: 35px;
      }
  }
  .login-btn button {
      width: 100%;
      height: 48px;
      margin-top: 60px;
      border-radius:10px;
      background-color: #6594ee;
      font-weight: 600;
      font-size: 18px;
  }
  </style>
